﻿using System;
using System.Collections.Generic;
using System.Linq;
using BattleCity.Classes;
using BattleCity.GUI;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;

namespace BattleCity.GameObjects
{
    public class Rocket : gBaseClass
    {
        //анимации снаряда
        Animation _moveUp;
        Animation _moveDown;
        Animation _moveLeft;
        Animation _moveRight;

        //Звуки снаряда
        SoundEffect _blockSound;
        SoundEffect _eraseSound;
        SoundEffect _shootSound;

        //Объект производивший выстрел
        Tank _shooter;

        bool _isSuper;
        Direction _dir;

        public event EventHandler Exploded;

        public Rocket(BattleCityGame game, Tank shooter, Vector2 sprPosition, float speed, bool super, Direction direction)
            : base(game, ref game.newGame.shootSprite, game.SpriteRec16, 0.7f)
        {
            LoadSounds(game.ShootSoundList);
            LoadAnimations(game.ShootAnimList);
            _isSuper = super;
            _speed = speed;
            _dir = direction;
            this.SprPosition = DeterminePosition(sprPosition, _dir);
            _shooter = shooter;
        }
        Vector2 DeterminePosition(Vector2 Position, Direction dir)
        {
            switch (dir)
            {
                case Direction.Up:
                    return new Vector2(Position.X + 8f, Position.Y - 15f);
                case Direction.Down:
                    return new Vector2(Position.X + 8f, Position.Y + 15f);
                case Direction.Left:
                    return new Vector2(Position.X - 15f, Position.Y + 8f);
                case Direction.Right:
                    return new Vector2(Position.X + 15f, Position.Y + 8f);
                default:
                    return Position;
            }
        }
        void LoadAnimations(Dictionary<string, Animation> animList)
        {
            _moveUp = animList["moveUp"];
            _moveDown = animList["moveDown"];
            _moveLeft = animList["moveLeft"];
            _moveRight = animList["moveRight"];
        }
        void LoadSounds(Dictionary<string, SoundEffect> soundList)
        {
            _blockSound = soundList["blockSound"];
            _eraseSound = soundList["eraseSound"];
            _shootSound = soundList["shootSound"];
        }
        public override void Initialize()
        {
            switch (_dir)
            {
                case Direction.Up:
                    _sprite.PlayAnimation(_moveUp);
                    break;
                case Direction.Down:
                    _sprite.PlayAnimation(_moveDown);
                    break;
                case Direction.Left:
                    _sprite.PlayAnimation(_moveLeft);
                    break;
                case Direction.Right:
                    _sprite.PlayAnimation(_moveRight);
                    break;
            }
            _shootSound.Play();
            base.Initialize();
        }
        public void CheckCollision()
        {
            var objects = from c in Game.Components
                          where IsCollideWithObject(c as gBaseClass) && !c.Equals(this) && !(c is Screen)
                          select (gBaseClass)c;
            foreach (gBaseClass spr in objects)
            {
                if (spr.GetType() == (typeof(Rocket)))
                {
                    if (spr.SprPosition != this.SprPosition)
                    {
                        _blockSound.Play();
                        if (Exploded != null)
                            Exploded(this, new EventArgs());
                        spr.Dispose();
                    }
                }
                else if (spr.GetType() == (typeof(Wall)))
                {
                    _eraseSound.Play();
                    if (Exploded != null)
                        Exploded(this, new EventArgs());
                    spr.Dispose();
                }
                else if (spr.GetType() == (typeof(Stone)))
                {
                    if (_isSuper)
                    {
                        _eraseSound.Play();
                        spr.Dispose();
                    }
                    else
                        _blockSound.Play();
                    if (Exploded != null)
                        Exploded(this, new EventArgs());
                }
                else if (spr.GetType() == (typeof(Base)))
                {
                    _eraseSound.Play();
                    if (Exploded != null)
                        Exploded(this, new EventArgs());
                    spr.Kill();
                }
                else if (spr.GetType() == (typeof(Enemy)) && _shooter.GetType() == typeof(Me))
                {
                    Enemy en = spr as Enemy;
                    en.Kill();
                    if (Exploded != null)
                        Exploded(this, new EventArgs());
                }
                else if (spr.GetType() == (typeof(Me)) && _shooter.GetType() == typeof(Enemy))
                {
                    Me pl = spr as Me;
                    if (!pl._isGod)
                        pl.Kill();
                    if (Exploded != null)
                        Exploded(this, new EventArgs());
                }
                break;
            }
            if (CheckBounds())
            {
                _blockSound.Play();
                if (Exploded != null)
                    Exploded(this, new EventArgs());
            }
        }
        void Move()
        {
            switch (_dir)
            {
                case Direction.Up:
                    MoveUp(_speed);
                    _sprite.PlayAnimation(_moveUp);
                    break;
                case Direction.Down:
                    MoveDown(_speed);
                    _sprite.PlayAnimation(_moveDown);
                    break;
                case Direction.Left:
                    MoveLeft(_speed);
                    _sprite.PlayAnimation(_moveLeft);
                    break;
                case Direction.Right:
                    MoveRight(_speed);
                    _sprite.PlayAnimation(_moveRight);
                    break;
            }
        }
        public override void Update(GameTime gameTime)
        {
            Move();
            CheckCollision();
            base.Update(gameTime);
        }
    }
}